#!/bin/bash

# 接收参数
version=$1
ali_at=$2
ali_pd=$3

echo '删除容器'
container_id=$(docker ps -a | grep '/ts230712/' | awk '{print $1}')
if [ ! -z "$container_id" ]; then
  for container in $container_id
  do
    echo "处理容器:$container"
    docker rm -f $container
  done
fi

echo '删除镜像'
image_id=$(docker images | grep '/ts230712/' | awk '{print $3}')
if [ ! -z "$image_id" ]; then
  for image in $image_id
  do
    echo "处理镜像:$image"
    docker rmi -f $image
  done
fi

echo '登录harbor账号'
docker login -u $ali_at -p $ali_pd 18.18.18.98:80

echo '下载镜像'
docker pull 18.18.18.98:80/ts230712/gateway:$version
docker pull 18.18.18.98:80/ts230712/account:$version
docker pull 18.18.18.98:80/ts230712/album:$version
docker pull 18.18.18.98:80/ts230712/dispatch:$version
docker pull 18.18.18.98:80/ts230712/order:$version
docker pull 18.18.18.98:80/ts230712/payment:$version
docker pull 18.18.18.98:80/ts230712/search:$version
docker pull 18.18.18.98:80/ts230712/user:$version

echo '运行容器'
docker run -d -p 8090:8090 --name gateway 18.18.18.98:80/ts230712/gateway:$version
docker run -d -p 8505:8505 --name account 18.18.18.98:80/ts230712/account:$version
docker run -d -p 8501:8501 --name album 18.18.18.98:80/ts230712/album:$version
docker run -d -p 8509:8509 --name dispatch 18.18.18.98:80/ts230712/dispatch:$version
docker run -d -p 8504:8504 --name order 18.18.18.98:80/ts230712/order:$version
docker run -d -p 8506:8506 --name payment 18.18.18.98:80/ts230712/payment:$version
docker run -d -p 8502:8502 --name search 18.18.18.98:80/ts230712/search:$version
docker run -d -p 8503:8503 --name user 18.18.18.98:80/ts230712/user:$version

echo '部署完成，欢迎使用！！！'